package cn.itsource.org.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.basic.utils.PageResult;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.EmployeeService;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.mapper.LogininfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Employee> findAll() {
        return employeeMapper.findAll();
    }

    @Override
    public Employee findOne(Long id) {
        return employeeMapper.findOne(id);
    }

    @Transactional
    @Override
    public void add(Employee employee) {
        String salt = RandomUtil.randomString(32);
        String md5pws = SecureUtil.md5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5pws);
        Logininfo logininfo = new Logininfo();
        BeanUtil.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfoMapper.add(logininfo);
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.add(employee);

        //添加当前员工信息
        if (employee.getRoleId() != null){
            employeeMapper.addEmployeeRole(employee);
        }
    }

    @Transactional
    @Override
    public void update(Employee employee) {

        Logininfo logininfo = new Logininfo();
        BeanUtil.copyProperties(employee,logininfo);
        logininfo.setId(employee.getLogininfoId());
        logininfo.setType(0);
        logininfoMapper.update(logininfo);
        employeeMapper.update(employee);

        //先删除员工角色信息
        employeeMapper.deleteEmployeeRole(employee.getId());
        //添加当前员工信息
        if (employee.getRoleId() != null){
            employeeMapper.addEmployeeRole(employee);
        }
    }

    @Transactional
    @Override
    public void delete(Long id) {
        //删除员工角色信息
        employeeMapper.deleteEmployeeRole(id);

        Employee employee1 = employeeMapper.findOne(id);
        logininfoMapper.delete(employee1.getLogininfoId());
        employeeMapper.delete(id);
    }

    @Transactional
    @Override
    public void patchdel(Long[] ids){
        for (Long id : ids) {
            Employee employee1 = employeeMapper.findOne(id);
            logininfoMapper.delete(employee1.getLogininfoId());
        }
        employeeMapper.patchdel(ids);
    }

    @Override
    public PageResult<Employee> queryPage(EmployeeQuery employeeQuery) {
        Integer totals = employeeMapper.queryCount(employeeQuery);
        List<Employee> list = employeeMapper.queryData(employeeQuery);
        return new PageResult<Employee>(totals,list);
    }

    // 获取当前店铺下的所有员工：店长或者店员
    @Override
    public List<Employee> findEmployeeByShopId(Long shopId) {

        return employeeMapper.findEmployeeByShopId(shopId);
    }
}
